<template>
  <div>
  <el-table
       row-key="id"
       default-expand-all
       ref="multipleTable"
      :data="renderDynamic">
    <el-table-column type="selection" width="55"> </el-table-column>
    <el-table-column type="index" label="序号" width="50"> </el-table-column>
    <el-table-column prop="createUser" label="创建人"> </el-table-column>
  </el-table>
    <el-button @click="selectHandle">勾选</el-button>
  <el-table
      :data="renderDynamic"
      ref="product"
      row-key="id"
      size="mini"
      @select="selectRow"
      default-expand-all
      @select-all="selectAllRow"
      @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55"> </el-table-column>
    <el-table-column type="index" label="序号" width="50"> </el-table-column>
    <el-table-column prop="createUser" label="创建人"> </el-table-column>
    <el-table-column prop="fileName" label="文件名"> </el-table-column>
    <el-table-column prop="createTime" label="时间"> </el-table-column>
  </el-table>
  </div>
</template>
<script>
export default {
  name: "",
  data() {
    return {
      renderDynamic: [
        {
          id: "1589546710706933760",
          pid: "-1",
          fullName: "hdfs://mycluster:8020/dmp/meta/script/XX项目演示-孟/12",
          isDirectory: 1,
          description: "12",
          fileName: "12",
          size: 109881,
          createUser: "admin",
          createTime: "2022-11-06 00:14:03",
          updateUser: "admin",
          updateTime: "2022-11-06 00:14:03",
          sensitivity: null,
          standardNumber: null,
          sort: "",
          children: [
            {
              id: "1598575793801367552",
              pid: "1589546710706933760",
              fullName:
                  "hdfs://hdp01:8020/dmp/meta/script/XX项目演示-孟/12/4.png",
              isDirectory: 0,
              description: null,
              fileName: "4.png",
              size: 36627,
              createUser: "admin",
              createTime: "2022-12-02 01:12:24",
              updateUser: "admin",
              updateTime: "2022-12-02 01:12:24",
              sensitivity: null,
              standardNumber: "“”",
              sort: "",
              children: [],
            },
            {
              id: "1598583904029212672",
              pid: "1589546710706933760",
              fullName:
                  "hdfs://hdp01:8020/dmp/meta/script/XX项目演示-孟/12/3.png",
              isDirectory: 0,
              description: null,
              fileName: "3.png",
              size: 36627,
              createUser: "admin",
              createTime: "2022-12-02 01:44:38",
              updateUser: "admin",
              updateTime: "2022-12-06 15:18:44",
              sensitivity: 1,
              standardNumber: "",
              sort: "文件类型1",
              children: [
                {
                  id: "1589546834929635328",
                  pid: "-1",
                  fullName:
                      "hdfs://hdp01:8020/dmp/meta/script/XX项目演示-孟/12/1.jpg",
                  isDirectory: 0,
                  description: null,
                  fileName: "1.jpg",
                  size: 98316,
                  createUser: "admin",
                  createTime: "2022-11-07 17:14:32",
                  updateUser: "admin",
                  updateTime: "2022-11-07 17:14:32",
                  sensitivity: null,
                  standardNumber: null,
                  sort: "",
                  children: [
                    {
                      id: "1656206644327051264",
                      pid: "-1",
                      fullName:
                          "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0633—2022：教育基础数据.pdf",
                      isDirectory: 0,
                      description: null,
                      fileName: "JYT 0633—2022：教育基础数据.pdf",
                      size: 266646,
                      createUser: "admin",
                      createTime: "2023-05-10 15:56:49",
                      updateUser: "admin",
                      updateTime: "2023-05-10 15:56:49",
                      sensitivity: null,
                      standardNumber: "",
                      sort: "",
                      children: [],
                    },
                  ],
                },
              ],
            },
          ],
        },

        {
          id: "1656206805937778688",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0637—2022：教育系统人员基础数据.pdf",
          isDirectory: 0,
          description: null,
          fileName: "JYT 0637—2022：教育系统人员基础数据.pdf",
          size: 227038,
          createUser: "admin",
          createTime: "2023-05-10 15:57:28",
          updateUser: "admin",
          updateTime: "2023-05-10 15:57:28",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656206863827562496",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0639—2022：中小学校基础数据.pdf",
          isDirectory: 0,
          description: null,
          fileName: "JYT 0639—2022：中小学校基础数据.pdf",
          size: 238599,
          createUser: "admin",
          createTime: "2023-05-10 15:57:42",
          updateUser: "admin",
          updateTime: "2023-05-10 15:57:42",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656206911458078720",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0641—2022：智慧教育平台基本功能要求.pdf",
          isDirectory: 0,
          description: null,
          fileName: "JYT 0641—2022：智慧教育平台基本功能要求.pdf",
          size: 489197,
          createUser: "admin",
          createTime: "2023-05-10 15:57:53",
          updateUser: "admin",
          updateTime: "2023-05-10 15:57:53",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656207095046959104",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0644—2022：数字教育资源基础分类代码.pdf",
          isDirectory: 0,
          description: null,
          fileName: "JYT 0644—2022：数字教育资源基础分类代码.pdf",
          size: 345741,
          createUser: "admin",
          createTime: "2023-05-10 15:58:37",
          updateUser: "admin",
          updateTime: "2023-05-10 15:58:37",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656207200630173696",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0646—2022：教师数字素养.pdf",
          isDirectory: 0,
          description: null,
          fileName: "JYT 0646—2022：教师数字素养.pdf",
          size: 328113,
          createUser: "admin",
          createTime: "2023-05-10 15:59:02",
          updateUser: "admin",
          updateTime: "2023-05-10 15:59:02",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656207256464748544",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/JYT 0650—2022：智慧教育平台数字教育资源技术要求.pdf",
          isDirectory: 0,
          description: null,
          fileName: "JYT 0650—2022：智慧教育平台数字教育资源技术要求.pdf",
          size: 255907,
          createUser: "admin",
          createTime: "2023-05-10 15:59:15",
          updateUser: "admin",
          updateTime: "2023-05-10 15:59:15",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656207298957242368",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/关于印发智慧校园建设有关标准的通知.pdf",
          isDirectory: 0,
          description: null,
          fileName: "关于印发智慧校园建设有关标准的通知.pdf",
          size: 418195,
          createUser: "admin",
          createTime: "2023-05-10 15:59:26",
          updateUser: "admin",
          updateTime: "2023-05-10 15:59:26",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1656207334147452928",
          pid: "-1",
          fullName:
              "hdfs://mycluster:8020/dataStandard/file/教育行业数融平台/智慧校园 第2部分：高等教育阶段评价指标.pdf",
          isDirectory: 0,
          description: null,
          fileName: "智慧校园 第2部分：高等教育阶段评价指标.pdf",
          size: 427093,
          createUser: "admin",
          createTime: "2023-05-10 15:59:34",
          updateUser: "admin",
          updateTime: "2023-05-10 15:59:34",
          sensitivity: null,
          standardNumber: "",
          sort: "",
          children: [],
        },
        {
          id: "1660926004253716480",
          pid: "-1",
          fullName: "hdfs://mycluster:8020/dataStandard/file/验收项目/测试文件",
          isDirectory: 1,
          description: "1",
          fileName: "测试文件",
          size: 0,
          createUser: "admin",
          createTime: "2023-05-23 16:29:53",
          updateUser: "admin",
          updateTime: "2023-05-23 16:29:53",
          sensitivity: null,
          standardNumber: null,
          sort: "",
          children: [],
        },
      ],
    };
  },
  // isSelect状态：true为选中状态，false为未选中状态 ，空字符串为未知状态
  mounted() {},
  methods: {
    selectHandle(){
        let row = this.renderDynamic[0].children[0]
        console.log("row:",row)
        // if (rows) {
        //   rows.forEach(row => {
         //   this.$refs.multipleTable.toggleRowSelection(row);
          this.$refs.multipleTable.toggleRowSelection(row, true);
        //   });
        // } else {
        //   this.$refs.multipleTable.clearSelection();
        // }
    },
    // 勾选父节点时，子层级一起勾选或一起取消勾选
    selectRow(selection, row) {
      let data = selection.some((item) => {
        return row.id === item.id;
      });
      if (data) {
        // 勾选节点时
        if (row.children) {
          this.setChildren(row.children, true);
        }
      } else {
        // 取消勾选节点时
        if (row.children) {
          this.setChildren(row.children, false);
        }
      }
    },
    // 全选按钮
    selectAllRow(selection) {
      // 全选
      let isSelect = selection.some((item) => {
        let tableDataIds = this.renderDynamic.map((data) => data.id);
        if (tableDataIds.indexOf(item.id) !== -1) {
          return true;
        } else {
          return false;
        }
      });
      // 全不选
      let isCancel = !this.renderDynamic.every((item) => {
        let selectIds = selection.map((data) => data.id);
        if (selectIds.indexOf(item.id) !== -1) {
          return true;
        } else {
          return false;
        }
      });
      if (isSelect) {
        selection.map((item) => {
          if (item.children) {
            this.setChildren(item.children, true);
          }
        });
      }
      if (isCancel) {
        this.renderDynamic.map((item) => {
          if (item.children) {
            this.setChildren(item.children, false);
          }
        });
      }
    },
    // 父节点含多个子层级
    setChildren(children, type) {
      children.map((item) => {
        this.toggleSelection(item, type);
        if (item.children) {
          this.setChildren(item.children, type);
        }
      });
    },

    toggleSelection(row, select) {
      if (row) {
        this.$nextTick(() => {
          this.$refs.product.toggleRowSelection(row, select);
        });
      }
    },
    handleSelectionChange(val) {
      console.log(val, "选中的值");
    },
  },
};
</script>
